home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / bbskt30a.zip / TERM.PAS < prev    next >
Pascal/Delphi Source File  |  1993-11-10  |  19KB  |  678 lines

  1. {
  2.   Term.Pas
  3.  
  4.   A sample terminal program for BBSkit.
  5.  
  6.   Version 1.2; updated for BBSkit 3.0.
  7.  
  8.   Written by Steve Madsen
  9.  
  10.   This program also includes a couple of "features" for debugging.  Compile
  11.   with the symbol DEBUG defined for the extras.  They are:
  12.  
  13.      Press Alt-D in terminal mode for a dump of the UART registers and some
  14.       other useful stuff.
  15.  
  16.      Press Alt-I to retrigger the interrupts.  This generally restarts a
  17.       stopped transmission if there is a problem with the interrupt handler.
  18.       May have to hit it a few times, though.
  19.  
  20.      Press F2 to output a >200 character string.
  21.  
  22.      -Dx command line switch lets you open two ports at once.  The second
  23.       port is COMx and runs at the same bps rate (to start with) as the
  24.       standard port.  You must Alt-X out of both ports to quit the program.
  25.       Switch between them with Left Alt-F1 and Left Alt-F2.
  26.  
  27.   NOTE: intended to be compiled using the registered version of BBSkit.  If
  28.   you wish to recompile with a demo copy, remove the space before the $ in
  29.   the following $DEFINE.
  30. }
  31.  
  32. { $DEFINE DEMO}
  33.  
  34. PROGRAM Term12;
  35.  
  36. {$X+}
  37. {$M 16384, 0, 131072}
  38.  
  39. {$DEFINE NOBSP}
  40.  
  41. Uses CRT, DOS, VC, Protocol, BBSkit, Comm, Util, MTask;
  42.  
  43. Const
  44.   MaxEmu     = 4;
  45.   Emulations : Array[1..MaxEmu] of String = ('TTY', 'ANSI', 'VT100', 'VT52');
  46.  
  47. Type
  48.   TTerm = object(TBBS)
  49.     Baud      : Longint;
  50.     Capture   : Boolean;
  51.     Comport   : Byte;
  52.     TermFlags : TTermMode;
  53.     ExitCh    : Char;
  54.     Printer   : Boolean;
  55.     Template  : Byte;
  56.  
  57.     CONSTRUCTOR Init(IComport : Byte; IBaud : Longint);
  58.     PROCEDURE Run; VIRTUAL;
  59.     DESTRUCTOR Done; VIRTUAL;
  60.  
  61.     PROCEDURE Baudrate;
  62.     PROCEDURE DebugInfo;
  63.     PROCEDURE DOSShell;
  64.     PROCEDURE Download;
  65.     PROCEDURE Emulation;
  66.     PROCEDURE EnterAnswerMode;
  67.     PROCEDURE EnterOriginateMode;
  68.     PROCEDURE Help(var Cmd : Char);
  69.     PROCEDURE ReInitModem;
  70.     PROCEDURE Status(Msg : String);
  71.     PROCEDURE ToggleBackspace;
  72.     PROCEDURE ToggleCapture;
  73.     PROCEDURE ToggleDuplex;
  74.     PROCEDURE TogglePrinter;
  75.     PROCEDURE ToggleShowControls;
  76.     PROCEDURE Upload;
  77.   end;
  78.  
  79. Var
  80.   TaskID     : Word;
  81.   TaskResult : Word;
  82.   Term       : TTerm;
  83.   Param      : Word;
  84.  
  85. {$IFDEF DEBUG}
  86.   DebugTerm : TTerm;
  87. {$ENDIF}
  88.  
  89. {********************************************************************}
  90.  
  91. PROCEDURE Usage;
  92.  begin
  93.    WriteLn('Term usage:');
  94.    WriteLn;
  95.    WriteLn(ProgramName, ' <comport> <baudrate> [-o]');
  96.    WriteLn;
  97.    WriteLn(' <comport> can be 1, 2, 3 or 4.');
  98.    WriteLn(' <baudrate> can be 300, 600, 1200, 2400, 4800, 9600, 19200,');
  99.    WriteLn('                   38400, 57600, or 115200.');
  100.    WriteLn;
  101.    WriteLn(' -o   starts Term without sending the init string');
  102. {$IFDEF DEBUG}
  103.    WriteLn;
  104.    WriteLn(' -dx  opens debug port COMx at same speed.  Must be last parameter!');
  105. {$ENDIF}
  106.    WriteLn;
  107.    WriteLn('example: ', ProgramName, ' 2 2400    { com2, at 2400 bps }');
  108.    WriteLn('         ', ProgramName, ' 1 9600    { com1, at 9600 bps }');
  109.  end;
  110.  
  111. {--------------------------------------------------------------------}
  112.  
  113. PROCEDURE StartATerm(var AtPort); FAR;
  114.  begin
  115.    if (Word(AtPort) = 0) then
  116.     begin
  117.       Term.Init(StrToInt(ParamStr(1)), StrToInt(ParamStr(2)));
  118.       Term.Run;
  119.       Term.Done;
  120. {$IFDEF DEBUG}
  121.     end
  122.    else
  123.     begin
  124.       DebugTerm.Init(StrToInt(Copy(ParamStr(ParamCount), 3, 1)), StrToInt(ParamStr(2)));
  125.       DebugTerm.Run;
  126.       DebugTerm.Done;
  127. {$ENDIF}
  128.     end;
  129.  end;
  130.  
  131. {--------------------------------------------------------------------}
  132.  
  133. CONSTRUCTOR TTerm.Init(IComport : Byte; IBaud : Longint);
  134.  begin
  135.    TBBS.Init;
  136. {$IFDEF DEBUG}
  137.    AllowVCSwitching(True);
  138. {$ELSE}
  139.    AllowVCSwitching(False);
  140. {$ENDIF}
  141.    Comport := IComport;
  142.    Baud := IBaud;
  143.    if (not OpenPort(Comport)) then
  144.     begin
  145.       vcWriteLn('Can''t open comport.');
  146.       Halt(1);
  147.     end;
  148.    SetBpsRate(Comport, Baud);
  149.    SetFlowControl(PortIdx, False, False);
  150.    SetParity(PortIdx, NoParity);
  151.    SetWordLength(PortIdx, 8);
  152.    SetStopBits(PortIdx, 1);
  153.    TermFlags.Duplex := Full;
  154.    TermFlags.ShowControls := False;
  155.    TermFlags.Backspace := #8;
  156.    Capture := False;
  157.    Printer := False;
  158.    SetInput(True, False);
  159.    ClrScr;
  160.    Template := 1;
  161.    Status('');
  162.    if (ParamCount < 3) or (Lower(ParamStr(3)) <> '-o') then
  163.       EnterOriginateMode;
  164.  end;
  165.  
  166. {--------------------------------------------------------------------}
  167.  
  168. PROCEDURE TTerm.Run;
  169.  Const
  170.    BigString = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'+
  171.                'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'+
  172.                'cccccccccccccccccccccccccccccccccccccccccccccccccccccccc'+
  173.                'dddddddddddddddddddddddddddddddddddddddddddddddddddddddd';
  174.  
  175.  begin
  176.    Repeat
  177.      ExitCh := TerminalMode(TermFlags);
  178.      if (ExitCh = #59) then
  179.         Help(ExitCh);  { F1 = help }
  180.      case ExitCh of
  181. {$IFDEF DEBUG}
  182.        #60 : begin
  183.                SendString(PortIdx, BigString);
  184.                Status(IntToStr(PortArray[PortIdx].OutUsed));
  185.              end;
  186.        #32 : DebugInfo;
  187.        #23 : ReInitModem;
  188. {$ENDIF}
  189.        #48 : Baudrate;
  190.        #36 : DOSShell;
  191.        #81 : Download;
  192.        #50 : Emulation;
  193.        #30 : EnterAnswerMode;
  194.        #24 : EnterOriginateMode;
  195.        #35 : begin
  196.                Hangup;
  197.                Status('');
  198.              end;
  199.        #20 : ToggleBackspace;
  200.        #46 : ToggleCapture;
  201.        #18 : ToggleDuplex;
  202.        #25 : TogglePrinter;
  203.        #31 : ToggleShowControls;
  204.        #73 : Upload;
  205.      end;
  206.    Until (ExitCh = #45);  { Alt-X = quit }
  207.  end;
  208.  
  209. {--------------------------------------------------------------------}
  210.  
  211. DESTRUCTOR TTerm.Done;
  212.  Var
  213.    Online : Boolean;
  214.  
  215.  begin
  216.    Online := Carrier(PortIdx);
  217.    ClosePort(not Online);
  218.    Window(1, 1, 80, TextScreenMaxY);
  219.    TextColor(LightGray);
  220.    TextBackground(Black);
  221.    ClrScr;
  222.    TBBS.Done;
  223.    if (Online) and (InCommandLine('-D') = 0) then
  224.       WriteLn('Warning: DTR not lowered since you are still online.');
  225.  end;
  226.  
  227. {--------------------------------------------------------------------}
  228.  
  229.   {
  230.   *  We can just double the rate for any step up, *except* for the step
  231.   *  from 38400 to 57600.
  232.   }
  233.  
  234. PROCEDURE TTerm.Baudrate;
  235.  begin
  236.    if (Baud <> 38400) then
  237.     begin
  238.       Baud := Baud SHL 1;
  239.       if (Baud > 115200) then
  240.          Baud := 300;
  241.     end
  242.    else
  243.       Baud := 57600;
  244.    SetBpsRate(Comport, Baud);
  245.    Status('');
  246.  end;
  247.  
  248. {--------------------------------------------------------------------}
  249.  
  250. PROCEDURE TTerm.DebugInfo;
  251.  
  252.  FUNCTION BinaryByte(Value : Byte) : String;
  253.   Var
  254.     Strn : String;
  255.     Idx  : Word;
  256.  
  257.   begin
  258.     Strn := '';
  259.     Idx := $1;
  260.     Repeat
  261.       if (Value AND Idx = Idx) then
  262.          Strn := '1' + Strn
  263.       else
  264.          Strn := '0' + Strn;
  265.       Idx := Idx SHL 1;
  266.     Until (Idx = $100);
  267.     BinaryByte := Strn;
  268.   end;
  269.  
  270.  begin
  271.    vcWriteLn('');
  272.    vcWrite  ('   Port: COM' + IntToStr(PortIdx));
  273.    vcWrite  ('      Status flags: ' + BinaryByte(PortArray[Comport].StatusFlg));
  274.    vcWriteLn('   Error flags: ' + BinaryByte(PortArray[Comport].ErrorFlg));
  275.    vcWrite  ('    IER: ' + BinaryByte(Port[PortArray[Comport].PortAddr + IER]));
  276.    vcWrite  ('           IIR: ' + BinaryByte(Port[PortArray[Comport].PortAddr + IIR]));
  277.    vcWriteLn('           LCR: ' + BinaryByte(Port[PortArray[Comport].PortAddr + LCR]));
  278.    vcWrite  ('    MCR: ' + BinaryByte(Port[PortArray[Comport].PortAddr + MCR]));
  279.    vcWrite  ('           LSR: ' + BinaryByte(Port[PortArray[Comport].PortAddr + LSR]));
  280.    vcWriteLn('           MSR: ' + BinaryByte(Port[PortArray[Comport].PortAddr + MSR]));
  281.    vcWrite  ('    SCR: ' + BinaryByte(Port[PortArray[Comport].PortAddr + SCR]));
  282.    vcWrite  ('          OCW1: ' + BinaryByte(Port[OCW1]));
  283.    vcWriteLn('          OCW2: ' + BinaryByte(Port[OCW2]));
  284.    vcWrite  ('OutUsed: ' + Left(IntToStr(PortArray[Comport].OutUsed), 4));
  285.    vcWriteLn('            InUsed: ' + IntToStr(PortArray[Comport].InUsed));
  286.    vcWriteLn('');
  287.  end;
  288.  
  289. {---------